package leetcode.easy;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;

/**
 * ClassName: $349_Intersection
 * Description:
 * date: 2021-09-30 15:09
 *
 * @author liyifan
 */
public class $349_Intersection {
    public int[] intersection(int[] nums1, int[] nums2) {
        int[] arr = new int[1000];
        ArrayList<Integer> res = new ArrayList<>();
        for (int i = 0;i < nums1.length;i++) {
            if (arr[nums1[i]] == 0) {
                arr[nums1[i]] = 1;
            }
        }
        for (int i = 0;i < nums2.length;i++) {
            if (arr[nums2[i]] != 0 && !res.contains(nums2[i])) {
                res.add(nums2[i]);
            }
        }
        int[] arr_res = new int[res.size()];
        for (int i = 0;i < arr_res.length;i++) {
            arr_res[i] = res.get(i);
        }
        return arr_res;
    }

    public int[] intersection2(int[] nums1, int[] nums2) {
        HashSet<Integer> set = new HashSet<>();
        ArrayList<Integer> res_list = new ArrayList<>();
        for (int i = 0;i < nums1.length;i++) {
            set.add(nums1[i]);
        }
        for (int i = 0;i < nums2.length;i++) {
            if (set.contains(nums2[i]) && !res_list.contains(nums2[i])) {
                res_list.add(nums2[i]);
            }
        }
        int[] res = new int[res_list.size()];
        for (int i = 0;i < res_list.size();i++) {
            res[i] = res_list.get(i);
        }
        return res;
    }
}
